/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pennychecker.swing.presenter.ext.component;

import com.pennychecker.swing.mvp.event.HandlerRegistration;
import com.pennychecker.swing.mvp.event.MvpEvent;
import com.pennychecker.swing.presenter.ext.event.ChangeEvent;
import com.pennychecker.swing.presenter.ext.event.ChangeHandler;
import com.pennychecker.swing.presenter.ext.event.FocusEvent;
import com.pennychecker.swing.presenter.ext.event.FocusHandler;
import com.pennychecker.swing.presenter.ext.event.HasChangeHandlers;
import com.pennychecker.swing.presenter.ext.event.HasFocusHandlers;
import com.pennychecker.swing.presenter.ext.listener.DocumentListenerAdapter;
import com.pennychecker.swing.presenter.ext.listener.SwingFocusListenerAdapter;
import javax.swing.JTextArea;

/**
 *
 * @author iolaus
 */
public class STextArea extends JTextArea implements HasFocusHandlers, HasChangeHandlers {

    private static final long serialVersionUID = 1L;
    private final HandlerAdapter handlerAdapter = new HandlerAdapter();

    @Override
    public HandlerRegistration addFocusHandler(FocusHandler handler) {
        super.addFocusListener(new SwingFocusListenerAdapter(handlerAdapter));
        return handlerAdapter.addHandler(handler, FocusEvent.TYPE);
    }

    @Override
    public void fireEvent(MvpEvent<?> event) {
        handlerAdapter.fireEvent(event);
    }

    @Override
    public HandlerRegistration addChangeHandler(ChangeHandler handler) {
        super.getDocument().addDocumentListener(new DocumentListenerAdapter(handlerAdapter));
        return handlerAdapter.addHandler(handler, ChangeEvent.TYPE);
    }
}
